﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using log4net;
using System.Globalization;

namespace Tireguit.Nmon.Parser.Statisitic
{
    public class CPUElement:NmonStatElement
    {
        protected new ILog _log=LogManager.GetLogger(typeof(CPUElement));
        protected IDataParameter _name =null;
        protected IDataParameter _zzzz = null;
        protected IDataParameter _usertime = null;
        protected IDataParameter _systime = null;
        protected IDataParameter _waittime = null;
        protected IDataParameter _idletime = null;
        public string Name{get;protected set;}
        public string Zzzz {get;protected set;}
        public float Usertime{get;protected set;}
        public float Systime { get; protected set; }
        public float Waittime { get; protected set; }
        public float Idletime {get; protected set;}
        public CPUElement(IDbConnection connection)
        {
            Balise = BaliseType.CPU;
            ListCommand = new List<System.Data.IDbCommand>();
            Connection = connection;
            Linestring = string.Empty;
            _insertreq="INSERT INTO "+NmonTables.TablesDictionnary.Instance["CPU"]+" ([cpu],[zzzz],[Usertime],[Systime],[Waittime],[Idletime]) VALUES (@cpu,@zzzz,@Usertime,@Systime,@Waittime,@Idletime)";
            Command = Connection.CreateCommand();
            Command.CommandText = _insertreq;
            ListCommand.Add(Command);
            _name = Command.CreateParameter();
            _name.ParameterName = "@cpu";
            _name.DbType = DbType.String;
            _zzzz = Command.CreateParameter();
            _zzzz.ParameterName = "@zzzz";
            _zzzz.DbType = DbType.String;
            _usertime = Command.CreateParameter();
            _usertime.ParameterName = "@Usertime";
            _usertime.DbType = DbType.Decimal;
            _systime = Command.CreateParameter();
            _systime.ParameterName = "@Systime";
            _systime.DbType = DbType.Decimal;
            _waittime = Command.CreateParameter();
            _waittime.ParameterName = "@Waittime";
            _waittime.DbType = DbType.Decimal;
            _idletime = Command.CreateParameter();
            _idletime.ParameterName = "@Idletime";
            _idletime.DbType = DbType.Decimal;
        }
        public CPUElement():this(null){}
        public override List<IDbCommand> GetInsertReq()
        {
            try
            {
                _log.Debug("start GetInsertReq");


                if (Command == null)
                {
                    Command = Connection.CreateCommand();
                    Command.CommandText = _insertreq;
                    ListCommand.Add(Command);
                }
                Command.Parameters.Clear();
                _name.Value = Name;
                _zzzz.Value = Zzzz;
                _usertime.Value = Usertime;
                _systime.Value = Systime;
                _waittime.Value = Waittime;
                _idletime.Value = Idletime;
                Command.Parameters.Add(_name);
                Command.Parameters.Add(_zzzz);
                Command.Parameters.Add(_usertime);
                Command.Parameters.Add(_systime);
                Command.Parameters.Add(_waittime);
                Command.Parameters.Add(_idletime);

                return ListCommand;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetInsertReq", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop GetInsertReq");
            }
        }
        protected override bool InternalParse(string line)
        {
            try
            {
                _log.Debug("start InternalParse");
                if (string.IsNullOrEmpty(line))
                {
                    return false;
                }

                // AAAElement aaaelem = new AAAElement();
                string[] elements = line.Split(separ);
#if DEBUG
                if (!elements[0].StartsWith( "CPU"))
                {
                    _log.Error("Not CPU Line");
                    throw new ArgumentOutOfRangeException("start");
                }
#endif
                if (elements[1].StartsWith("CPU"))
                {
                    //Skip information header 
                    return false;
                }
                Name = elements[0];
                Zzzz = elements[1];
                float res = -1;
                if(float.TryParse(elements[2],NumberStyles.Float,CultureInfo.InvariantCulture ,out res))
                {
                    Usertime = res;

                }
                if (float.TryParse(elements[3], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Systime = res;

                }
                if (float.TryParse(elements[4], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Waittime = res;

                }
                if (float.TryParse(elements[5], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Idletime = res;

                }
                
                                
                return true;
                //  return aaaelem;

            }
            catch (System.Exception ex)
            {
                _log.Error("InternalParse", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop InternalParse");
            }
        }
    }
}
